package org.hive.testserver.domain.model;

import java.util.ArrayList;
import java.util.List;

public class Project implements Entity {
	private String name;
	private List<TestRun> testRuns;
	private ProjectHistory projectHistory;
	private ProjectConfiguration projectConfiguration;
	
	public Project(String name) {
		super();
		this.name = name;
	}
	
	public String getName() {
		return name;
	}
	
	public String getId() {
		return name;
	}

	public List<TestRun> getTestRuns() {
		//FIXME lazy initialization due to persistent strategy - review
		if (testRuns == null) {
			 testRuns = new ArrayList<TestRun>();
		}
		return testRuns;
	}

	public void addTestRun(TestRun testRun) {
		getTestRuns().add(testRun);
		testRun.setProject(this);
	}

	public void removeTestRun(TestRun testRun) {
		if (getTestRuns().remove(testRun)) {
			testRun.setProject(null);
		}
	}
	
	public ProjectHistory getProjectHistory() {
		initializeProjectHistoryIfNecessary();
		return projectHistory; 
	}

	private void initializeProjectHistoryIfNecessary() {
		// Compatibility with older persisted projects
		if (projectHistory == null) {
			 projectHistory = new ProjectHistory();
		}
	}

	public ProjectConfiguration getConfiguration() {
		initializeProjectConfigurationIfNecessary();
		return projectConfiguration;
	}

	private void initializeProjectConfigurationIfNecessary() {
		// Compatibility with older persisted projects
		if (projectConfiguration == null) {
			projectConfiguration = new ProjectConfiguration();
		}
	}
}
